"use strict";
this.AJAXLinks = function () {};

this.AJAXLinks.prototype = (function () {
    var init, thread, links, config, onConfigurationLoaded, initArea,
            initAnchor, onRedraw;

    init = function (webLayout) {
        links = [];
    };

    thread = function (webLayout) {};

    onConfigurationLoaded = function (webLayout, configuration) {
        var i, previousState, target, url, area;
        if (configuration[this.NAME] === undefined) {
            return;
        }
        config = configuration[this.NAME];
        for (i = config.length - 1; i >= 0; i -= 1) {
            initArea(config[i], webLayout, i);
        }
        if (!webLayout.getModule('HashStorage')) {
            return;
        }
        previousState = webLayout.getModule('HashStorage').load('AJAXLinks');
        if (previousState !== undefined) {
            area = config[previousState.area];
            if (area) {
                target = webLayout.xPath(area.target)[0];
                url = previousState.url.replace(new RegExp(area.match),
                        area.replace);
                webLayout.ajaxLib.load(url, '', target, function () {
                    webLayout.redraw(target);
                });
            }
        }
    };

    initArea = function (area, webLayout, areaIndex) {
        var areaLinks, target, i, j, initialized;
        areaLinks = webLayout.xPath(area.links);
        target = webLayout.xPath(area.target)[0];
        for (i = areaLinks.length - 1; i >= 0; i -= 1) {
            initialized = false;
            for (j = links.length - 1; j >= 0; j -= 1) {
                if (areaLinks[i] === links[j]) {
                    initialized = true;
                }
            }
            if (!initialized) {
                initAnchor(areaLinks[i], target, area, webLayout, areaIndex);
                links.push(areaLinks[i]);
            }
        }
    };

    onRedraw = function (webLayout, details) {
        var i;
        for (i = config.length - 1; i >= 0; i -= 1) {
            initArea(config[i], webLayout, i);
        }
    };

    initAnchor = function (link, target, area, webLayout, areaIndex) {
        webLayout.addEventListener(link, 'click', function (e) {
            var url;
            url = link.href.replace(new RegExp(area.match), area.replace);
            webLayout.moduleEvent('contentToBeReplaced', target);
            webLayout.ajaxLib.load(url, '', target, function (r, status, xhr) {
                if (status !== 200) {
                    return;
                }
                if (typeof webLayout.getModule('HashStorage') === 'object') {
                    webLayout.getModule('HashStorage').save('AJAXLinks', {
                        area: areaIndex,
                        url: link.href
                    });
                    webLayout.getModule('HashStorage').save('AJAXForms');
                }
                webLayout.redraw(target);
            });
            webLayout.preventDefault(e);
        });
    };

    return {
        NAME: 'AJAXLinks',
        init: init,
        thread: thread,
        onConfigurationLoaded: onConfigurationLoaded,
        onRedraw: onRedraw
    };
}());